package own.stu.jobgib.playown.netty.ch6;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.util.AttributeKey;

import java.nio.charset.Charset;

/**
 * @author
 */
public final class Server {

    public static Integer port_8077 = 8077;

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {

                            // ch.pipeline().addLast(new LineBasedFrameDecoder(30));
                            ch.pipeline().addLast(new StringLineBasedFrameDecoder(30));
                            ch.pipeline().addLast(new OutBoundHandlerA(), new OutBoundHandlerB(), new OutBoundHandlerC());
                            /*ch.pipeline().addLast(new OutBoundHandlerA());
                            ch.pipeline().addLast(new OutBoundHandlerB());
                            ch.pipeline().addLast(new OutBoundHandlerC());*/
                            /*ch.pipeline().addLast(new InBoundHandlerA());
                            ch.pipeline().addLast(new InBoundHandlerB());
                            ch.pipeline().addLast(new InBoundHandlerC());*/
                        }
                    });

            ChannelFuture f = b.bind(port_8077).sync();
            System.out.println(" server up ");
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    static class StringLineBasedFrameDecoder extends LineBasedFrameDecoder {

        private final Charset charset = Charset.defaultCharset();

        public StringLineBasedFrameDecoder(int maxLength) {
            super(maxLength);
        }

        public StringLineBasedFrameDecoder(int maxLength, boolean stripDelimiter, boolean failFast) {
            super(maxLength, stripDelimiter, failFast);
        }

        /*@Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            Object decoded = decode(ctx, in);
            if (decoded != null) {
                out.add(((ByteBuf) decoded).toString(charset));
            }
        }*/
    }
}